package de.htwg_konstanz.infen.aufg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;

public enum CreateDataGuideHelper {

	INSTANCE;

	private static final HashMap<String, CustomHashMapValueItem> outputMap = new HashMap<String, CustomHashMapValueItem>();

	public HashMap<String, CustomHashMapValueItem> getDataGuide(File _xmlFile)
			throws DocumentException, FileNotFoundException {

		Document doc = Dom4JDocHelper.getDocFromXML(_xmlFile);
		initTreeWalk(doc);
		return outputMap;
	}

	public HashMap<String, CustomHashMapValueItem> getDataGuide(String _xmlPath)
			throws DocumentException, FileNotFoundException {

		Document doc = Dom4JDocHelper.getDocFromXML(_xmlPath);
		initTreeWalk(doc);
		return outputMap;
	}

	private void initTreeWalk(Document doc) {
		outputMap.put(Dom4JDocHelper.transform2CustomPath(doc.getRootElement()
				.getPath()), CustomItemHelper.buildItem(Dom4JDocHelper
				.transform2CustomPath(doc.getRootElement().getPath())));
		treeWalk(doc.getRootElement());
	}

	private void treeWalk(Element element) {
		for (int i = 0, size = element.nodeCount(); i < size; i++) {
			Node node = element.node(i);
			if (node instanceof Element) {
				outputMap.put(Dom4JDocHelper.transform2CustomPath(node
						.getPath()), CustomItemHelper.buildItem(Dom4JDocHelper
						.transform2CustomPath(node.getPath())));
				treeWalk((Element) node);
			}
		}
	}
}
